package com.lzz.controller;

import com.github.pagehelper.PageInfo;
import com.lzz.entity.User;
import com.lzz.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * @author zhenzhen
 * @date 2018-12-21 下午3:51
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequiresRoles("admin")
    @RequiresPermissions("user:view")
    @GetMapping(value = "/index")
    public String index() {
        return "index";
    }

    @RequiresRoles("admin")
    @RequiresPermissions("user:addView")
    @RequestMapping(value = "/addUserView", method = RequestMethod.GET)
    public String addUserView() {
        return "addUser";
    }

    @PostMapping(value = "/addUser", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addUser(User user) {
        int result = userService.addUser(user);
        return result > 0 ? "添加成功" : "添加失败";
    }

    @GetMapping(value = "/getUser/{uuid}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public User getUserById(@PathVariable String uuid) {
        // 477ca336050411e9815274e543f95f28
        User user = userService.getUserById(uuid);
        return user;
    }

    @RequiresRoles("admin")
    @RequiresPermissions("user:page")
    @RequestMapping(value = "/page/{num}/{size}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public PageInfo<User> getUserforPage(@PathVariable int num, @PathVariable int size) {
        return userService.getUserForPage(num, size);
    }
    @GetMapping(value = "/getUserByUserName/{username}", produces = "application/json;charset=utf-8")
    @ResponseBody
    public User getUserByUserName(@PathVariable String username) {
        User user = userService.getUserByUserName(username);
        System.out.println(user.getUsername());
        System.out.println(user.getName());
        System.out.println(user.getPassword());
        return userService.getUserByUserName(username);
    }

    @GetMapping(value = "/loginView")
    public String loginView() {
        return "login";
    }
    @PostMapping(value = "/login")
    public String login(HttpServletRequest request, String username, String password, Map<String, String> msg) {
        // 客户端传送的用户信息
        Subject subject = SecurityUtils.getSubject();

        // 初始化一个验证token，就是封装了用户名和密码
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username, password);

        // 进行验证
        try {
            subject.login(usernamePasswordToken);
            msg.put("info", "登陆成功");
            User user = userService.getUserByUserName(username);
            HttpSession session = request.getSession();

            session.setAttribute("user", user.getUsername());

            String usernameSession = (String) session.getAttribute("user");
            System.out.println("===========" + usernameSession);
        } catch (UnknownAccountException e) {
            msg.put("info", "没有此账号");
        } catch (IncorrectCredentialsException e) {
            System.out.println("======="+e.getMessage());
            System.out.println(e.getClass().getName()+"===================");
            msg.put("info", "密码错误");
        }
        return "index";
    }

    @GetMapping(value = "/logout")
    public String logout(HttpSession session) {
        if (session.getAttribute("user") != null) {
            session.removeAttribute("user");
            // session.invalidate();
        }
        return "login";
    }

}
